package com.beasts.ant.page.controller;

import com.beasts.ant.page.common.dto.AntUCDTO;
import com.beasts.ant.page.common.token.AntUCToken;
import com.beasts.ant.page.entity.User;
import com.beasts.ant.page.service.IAuthLoginService;
import com.beasts.ant.page.utils.json.JacksonUtil;
import com.beasts.ant.page.utils.servlet.CookieUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 登录验证
 *
 * @author Locker
 * @date 28/05/2018 3:53 PM
 * @since 1.0
 */
@Controller
@RequestMapping("/antuc")
public class AntUCController {

    @Autowired
    private IAuthLoginService authLoginService;

    /**
     * 登录
     */
    @RequestMapping("/login")
    @ResponseBody
    public Map<String, Object> authLogin(HttpServletRequest req, HttpServletResponse resp) {
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        Map<String, Object> map = new HashMap<>();
        try {

            String fullToken = AntUCToken.getToken(username, password);
            String token = (String) JacksonUtil.getProperty(fullToken, "access_token");

            // 设置返回值
            User user = authLoginService.authLogin(username, password);

            String[] ss = {"1", "2", "21", "7", "5", "51", "52", "53"};

            AntUCDTO antUCDTO = new AntUCDTO();
            antUCDTO.setId("33");
            antUCDTO.setUsername(user.getUsername());
            antUCDTO.setPermissionsRole("admin");
            antUCDTO.setPermissionsVisit(Arrays.asList(ss));


            map.put("user", antUCDTO);
            map.put("token", token);

            // 设置cookie
            Map<String, Object> cookieMap = new HashMap<>();
            cookieMap.put("id", "33");
            cookieMap.put("deadline", System.currentTimeMillis());
            cookieMap.put("token", token);
            CookieUtil.setCookie(req, resp, "token", JacksonUtil.mapToJson(cookieMap), true);

            map.put("success", true);
            map.put("message", "登录成功");

            return map;
        } catch (Exception e) {
            resp.setStatus(401);
            return map;
        }
    }

    /**
     * 查询用户
     */
    @RequestMapping("/user")
    @ResponseBody
    public Map<String, Object> findUser(HttpServletRequest req, HttpServletResponse resp) {
        Map<String, Object> map = new HashMap<>();
        AntUCDTO antUCDTO = new AntUCDTO();

        String cookieStr = CookieUtil.getCookieValue(req, "token", true);
        Map<String, Object> cookieMap = JacksonUtil.json2map(cookieStr);

        String token = "";

        // 获取cookie.token
        try {
            token = (String) cookieMap.get("token");
            Map<String, Object> userMap = AntUCToken.getUserByToken(token);
            antUCDTO.setUsername((String) userMap.get("username"));
            antUCDTO.setStatusCode(200);
            antUCDTO.setMessage("用户已登录");
            antUCDTO.setPermissionsRole("admin");
            String[] visit = {"1", "2", "21", "7", "5", "51", "52", "53"};
            antUCDTO.setPermissionsVisit(Arrays.asList(visit));
            map.put("success", true);
            map.put("user", antUCDTO);
            return map;
        } catch (Exception e) {
            e.printStackTrace();
            map.put("success", false);
            return map;
        }
    }


}
